﻿Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Media.Imaging
Imports CommunityToolkit.Mvvm.ComponentModel
Imports CommunityToolkit.Mvvm.Input

Public Class ProfilePageViewModel : Inherits ObservableObject

    Private ReadOnly _userAvatarUrlBase As String = NavigationData.SERVER_ACCESS_URL + "/vdm/api/file/download/"

    Private _user As User
    Public Property User As User
        Get
            Return _user
        End Get
        Set(value As User)
            SetProperty(_user, value)
            If User.Avatar Is Nothing Then
                UserAvatar = New BitmapImage(New Uri("/VMDWebFrontend;component/Assets/SPGLP_icon.jpg", UriKind.Relative))
            Else
                UserAvatar = New BitmapImage(New Uri(_userAvatarUrlBase + User.Avatar, UriKind.Absolute))
            End If
        End Set
    End Property

    Private _userAvatar As BitmapImage
    Public Property UserAvatar As BitmapImage
        Get
            Return _userAvatar
        End Get
        Set(value As BitmapImage)
            SetProperty(_userAvatar, value)
        End Set
    End Property

    Private ReadOnly _client As HttpClientBase

    Public Property ToProfileEditPageCommand As New RelayCommand(AddressOf ToProfileEditPageClicked)
    Public Property ToChangePasswordPageCommand As New RelayCommand(AddressOf ToChangePasswordPageClicked)
    Public Property ToDeleteAccountPageCommand As New RelayCommand(AddressOf ToDeleteAccountPageClicked)
    Public Property LogOutCommand As New RelayCommand(AddressOf LogOutClicked)

    Public Sub New()
        '读取模拟的用户数据
        'User = UserSim
        'If Application.Current.Resources("UserId") IsNot Nothing Then
        '    '从资源文件中读取用户ID（测试是否在登录时成功写入）
        '    Console.WriteLine("User ID is " + CType(Application.Current.Resources("UserId"), String))
        'End If
        'Console.WriteLine("User Avater is " + UserAvatar.ToString())

        '从服务器中读取用户数据
        _client = New HttpClientBase()
        Call InitProfile()
    End Sub

    Private Async Function InitProfile() As Task
        '从服务器中读取用户数据
        Try
            If Application.Current.Resources("UserId") Is Nothing Then
                MessageBox.Show("登录状态异常，请重新登录")
                NavigationService.Navigate(NavigationData.LOGIN_PAGE_URI)
                Return
            End If
            Dim result = Await _client.GetAsync(Of User)("vdm/api/account/findUser/id/" + Application.Current.Resources("UserId").ToString())
            User = result.Data
        Catch ex As Exception
            Console.WriteLine(ex.StackTrace)
            MessageBox.Show($"系统错误：{ex.Message}")
            NavigationService.Navigate(NavigationData.LOGIN_PAGE_URI)
            Return
        End Try
    End Function

    Private Sub ToProfileEditPageClicked()
        '跳转到编辑页面
        NavigationService.Navigate(Of ProfileEditPage)()
    End Sub

    Private Sub ToChangePasswordPageClicked()
        '跳转到修改密码页面
        NavigationService.Navigate(Of ChangePasswordPage)()
    End Sub

    Private Sub ToDeleteAccountPageClicked()
        '跳转到删除账户页面
        NavigationService.Navigate(Of DeleteAccountPage)()
    End Sub

    Private Sub LogOutClicked()
        '退出登录
        If Not MessageBox.Show("确认退出登录吗？", MessageBoxButton.OKCancel) = MessageBoxResult.OK Then
            Exit Sub
        End If
        If Application.Current.Resources("UserId") IsNot Nothing Then
            Application.Current.Resources.Remove("UserId")
        End If
        NavigationService.Navigate(Of LoginPage)()
    End Sub

End Class
